//
//  YDPayAlertView.m
//  PeiZiWangBei
//
//  Created by Ju on 2017/11/20.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDPayAlertView.h"
#import "OttoKeyboardView.h"


#define kTextInputCount 6

@interface YDPayAlertView ()<UITextFieldDelegate>

@property (nonatomic, strong) OttoTextField *textfield;
@property (nonatomic, copy) InputFinishBlock finishBlock;
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) NSMutableArray * blackPointArr;


@end

@implementation YDPayAlertView


- (instancetype)initWithFrame:(CGRect)frame inputFinish:(InputFinishBlock)block
{
    self.finishBlock = block;
    
    if (self = [super initWithFrame:frame]) {
        
       
        UIView *maskView = [[UIView alloc] initWithFrame:self.bounds];
        maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        
        [maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(maskViewDidTap:)]];
        
        [self addSubview:maskView];
        [self mainView];
        
    }
    
    return self;
    
}



#pragma mark - method

- (void)show
{
     [[UIApplication sharedApplication].keyWindow addSubview:self];
    
}

- (void)hide
{
    [self removeFromSuperview];
}
- (void)textFieldDidChanged:(UITextField *)textField
{
    for (UIView *view in self.blackPointArr) {
        view.hidden = YES;
    }
    
    for (int i  = 0; i < textField.text.length; i++) {
        ((UIView *)[self.blackPointArr objectAtIndex:i]).hidden = NO;
    }
    
    if (textField.text.length == kTextInputCount) {
        if (self.finishBlock) {
            [self hide];
            self.finishBlock(textField.text);
        }
    }
    
}

- (void)tapClose:(UITapGestureRecognizer *)tap
{
    [self hide];
}
- (void)maskViewDidTap:(UITapGestureRecognizer *)tap
{
    [self removeFromSuperview];
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 
    if ([textField isFirstResponder]) {
        if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
            return NO;
        }
    }
    
    if([string isEqualToString:@"\n"]) {
        //按回车关闭键盘
        [textField resignFirstResponder];
        return NO;
    }
    
    if(string.length == 0) {
        //判断是不是删除键
        return YES;
    }
    
    if(textField.text.length >= kTextInputCount) {
        //输入的字符个数大于6，则无法继续输入
        return NO;
    }
    
    return YES;
}


#pragma mark - getter

-(OttoTextField *)textfield
{
    if (!_textfield) {
        
        CGFloat textFieldY = 74;
        CGFloat textFieldH = 44;
        
        _textfield = [[OttoTextField alloc] init];
        [_textfield becomeFirstResponder];
        _textfield.delegate = self;
        _textfield.textColor = [UIColor clearColor];
        _textfield.tintColor = [UIColor clearColor];
        _textfield.layer.borderWidth = 1;
        _textfield.layer.borderColor = [UIColor grayColor].CGColor;
        _textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textfield.noPaste = YES;
        
        [_textfield setKeyboardType:KeyboardTypeNumber];
        [_textfield setNumberKeyboardType:NumberKeyboardTypePay];
        [_textfield addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
        
        
        [_mainView addSubview:_textfield];
        [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(@(textFieldLeft));
//            make.right.equalTo(@(-textFieldLeft));
//            make.top.equalTo(@74);
//            make.height.equalTo(@44);
            make.width.equalTo(@(rate375(288)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(textFieldH)));
                make.top.equalTo(self.mainView).offset(rateX812(textFieldY));
            }else{
                make.height.equalTo(@(rate750(textFieldH)));
                make.top.equalTo(self.mainView).offset(rate750(textFieldY));
            }
            make.centerX.equalTo(self.mainView);
            
            
        }];
        CGFloat eachW = (LKScreenW - rate375(43) *2) / kTextInputCount;
        
        
        for (int i = 0; i < kTextInputCount - 1; i++) {
            
            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(eachW + i * eachW , rate750(11), 1, rate375(22))];
            [self.textfield addSubview:line];
            line.backgroundColor = [UIColor grayColor];
//            [line mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.centerY.equalTo(_textfield);
//            }];
            
        }
        
        CGFloat pointW = 20;
        CGFloat leftGap = (eachW - pointW) / 2;
        
        for (int i = 0; i < kTextInputCount; i ++) {
            UIView *blackPointView = [[UIView alloc] initWithFrame:CGRectMake(eachW * i + leftGap, rate750(12), pointW, pointW)];
            blackPointView.backgroundColor = [UIColor blackColor];
            blackPointView.hidden = YES;
            blackPointView.layer.cornerRadius = pointW/2;
            [self.textfield addSubview:blackPointView];
            [self.blackPointArr addObject:blackPointView];
                                      
        }
       
    }
    return _textfield;
    
}

- (UIView *)mainView
{
    
    
    if (!_mainView) {
        
        CGFloat leftGap = 22;
        _mainView = [[UIView alloc] init];
        _mainView.backgroundColor = [UIColor whiteColor];
        _mainView.layer.cornerRadius = 8;
        [self addSubview:_mainView];
        
        [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rate375(leftGap)));
            make.right.equalTo(@(rate375(-leftGap)));
            make.top.equalTo(@(rate375(156)));
            make.height.equalTo(@(rate375(152)));
            
        }];
       
        
        UIImageView *closeIcon = [[UIImageView alloc] init];
        closeIcon.userInteractionEnabled = YES;
        closeIcon.image = [UIImage imageNamed:@"zhifu-quxiao"];
        [_mainView addSubview:closeIcon];
        [closeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@26);
            make.top.equalTo(@24);
        }];
        
        UIView *tapView = [UIView new];
        tapView.userInteractionEnabled = YES;
        
        [_mainView addSubview:tapView];
        [tapView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(_mainView);
            make.height.width.equalTo(@40);
        }];
        
        [tapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClose:)]];
        
        
        
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.text = @"请输入支付密码";
        [_mainView addSubview:titleLbl];
        titleLbl.textAlignment = NSTextAlignmentCenter;
        titleLbl.font = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
        titleLbl.textColor = [UIColor colorWithHexString:dark];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@11);
            make.centerX.equalTo(_mainView);

        }];
        
       
        
        CGFloat lineLeftGap = 21;
        UILabel *line = [[UILabel alloc] init];
        [_mainView addSubview:line];
        line.backgroundColor = NumberColor(239, 239, 239);
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@50);
            make.left.equalTo(@(lineLeftGap));
            make.right.equalTo(@(-lineLeftGap));
            make.height.equalTo(@1);
        }];
        
        [self textfield];
        
    }
    return _mainView;


}

-(NSMutableArray *)blackPointArr
{
    if (!_blackPointArr) {
        _blackPointArr = [[NSMutableArray alloc] initWithCapacity:kTextInputCount];
    }
    return _blackPointArr;
}
@end
